//全局变量
var _pageIndex = 1 //当前页
var _pageSize = 6 //每页显示多少数据
var _quantity //总记录
var _pageId //分页所在的DIV
var _endPage //最后一页


/**
 * 初始化分页方法，使用此工具调用此方法即可
 * quntity:数据数量
 * pageId:分页DIV的ID
 * */
function initPagin(quantity, pageId) {
	_quantity = quantity
	_pageId = pageId
	_endPage = Math.ceil(quantity / _pageSize)
	createPagin(_pageIndex, _pageSize, _endPage, _pageId)
}

/**
 * pageIndex:当前页
 * pageSize:每页记录条数
 * endPage:最后一页
 * pageId:分页div的id
 * */
function createPagin(pageIndex, pageSize, endPage, pageId) {
	var pagin = document.querySelector("#" + pageId)
	pagin.innerHTML = " <a href='#' onclick='setPageIndex(" + (pageIndex == 1 ? 1 : pageIndex - 1) + ")' class='page'>上一页</a> "
	pagin.innerHTML += " <a href='#' onclick='setPageIndex(1)' class='page'>1</a> "
	if (endPage <= 12) {
		pagin.innerHTML += createPaginFor(2, endPage - 1)
	} else if (pageIndex <= 7) {
		pagin.innerHTML += createPaginFor(2, 12)
		pagin.innerHTML += " ... "
	} else if (pageIndex >= endPage - 6) {
		pagin.innerHTML += " ... "
		pagin.innerHTML += createPaginFor(endPage - 11, endPage - 1)
	} else {
		pagin.innerHTML += " ... "
		pagin.innerHTML += createPaginFor(pageIndex - 5, pageIndex + 5)
		pagin.innerHTML += " ... "
	}
	if (endPage != 1)
		pagin.innerHTML += " <a href='#' onclick='setPageIndex(" + endPage + ")' class='page'>" + endPage + "</a> ";
	pagin.innerHTML += " <a href='#' onclick='setPageIndex(" + (pageIndex == endPage ? endPage : pageIndex + 1) + ")' class='page'>下一页</a> "
	setPageCss()
}

function createPaginFor(begin, end) {
	var msg = "";
	for (var i = begin; i <= end; i++) {
		msg += " <a href='#' onclick='setPageIndex(" + i + ")' class='page'>" + i + "</a> "
	}
	return msg;
}

//分页触发的事件
function setPageIndex(pageIndex) {
	_pageIndex = pageIndex

	//alert(pageIndex)
	createPagin(_pageIndex, _pageSize, _endPage, _pageId)
	app.ulist = getUserPagin(pageIndex, pageSize)
}



//设置分页样式
function setPageCss() {
	var pages = document.querySelectorAll("a[class='page']")
	for (var i = 0; i < pages.length; i++) {
		if (pages[i].innerHTML == _pageIndex)
			setAtStyle(pages[i])
		else
			setAllStyle(pages[i])
	}
}

//设置所有样式
function setAllStyle(aid) {

}

//设置当前选择的样式
function setAtStyle(aid) {

}


//滚动条在Y轴上的滚动距离
function getScrollTop() {
	var scrollTop = 0,
		bodyScrollTop = 0,
		documentScrollTop = 0
	if (document.body) {
		bodyScrollTop = document.body.scrollTop
	}
	if (document.documentElement) {
		documentScrollTop = document.documentElement.scrollTop
	}
	scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop
	return scrollTop
}

//文档的总高度
function getScrollHeight() {
	var scrollHeight = 0,
		bodyScrollHeight = 0,
		documentScrollHeight = 0;
	if (document.body) {
		bodyScrollHeight = document.body.scrollHeight
	}
	if (document.documentElement) {
		documentScrollHeight = document.documentElement.scrollHeight
	}
	scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
	return scrollHeight
}

//浏览器视口的高度
function getWindowHeight() {
	var windowHeight = 0
	if (document.compatMode == "CSS1Compat") {
		windowHeight = document.documentElement.clientHeight
	} else {
		windowHeight = document.body.clientHeight
	}
	return windowHeight
}


//创建滚动条监听,直接用这个方法
function createRollLoad() {
	window.onscroll = function () {
		if (getScrollTop() + getWindowHeight() == getScrollHeight()) {
			alert("已经到底部了！")
		}
	}
}